define(['common', 'toastr', 'bootstrapTable', 'bootstrapTableZh', 'jqueryValidationZh'], function(common, toastr) {
    var kindList = [];
    var wishList = [];
    var musicList = [];

    function initFun(code) {
        code = code ? code : '';
        (code.indexOf('kind') !== -1) || common.handleAjax('kind', { code: 1 }, true, true).done(function(result) {
            if (result.code === -4 || result.code === -1) {
                result = [];
            }
            result.forEach(function(item, index) {
                item.option = '<a onclick="scope.addKind(' + index + ')">修改</a> | <a onclick="scope.deleteKind(' + index + ')">删除</a>';
            });
            kindList = result;
            $("#kindTableData").bootstrapTable('destroy').bootstrapTable({
                pagination: true,
                pageSize: 5,
                pageList: [5],
                data: result
            });
        });
        (code.indexOf('wish') !== -1) || common.handleAjax('wish', { code: 1 }, true, true).done(function(result) {
            if (result.code === -4 || result.code === -1) {
                result = [];
            }
            result.forEach(function(item, index) {
                item.option = '<a onclick="scope.addWish(' + index + ')">修改</a> | <a onclick="scope.deleteWish(' + index + ')">删除</a>';
            });
            wishList = result;
            $("#wishTableData").bootstrapTable('destroy').bootstrapTable({
                pagination: true,
                pageSize: 5,
                pageList: [5],
                data: result
            });
        });
        (code.indexOf('music') !== -1) || common.handleAjax('music', { code: 1 }, true, true).done(function(result) {
            if (result.code === -4 || result.code === -1) {
                result = [];
            }
            result.forEach(function(item, index) {
                item.option = '<a onclick="scope.deleteMusic(' + index + ')">删除</a>';
            });
            musicList = result;
            $("#musicTableData").bootstrapTable('destroy').bootstrapTable({
                pagination: true,
                pageSize: 5,
                pageList: [5],
                data: result
            });
        });
    }

    return {
        init: initFun,
        addKind: function(index) {
            require(['text!../pages/base/addKind.html'], function(tpl) {
                layer.open({
                    title: index ? '添加类型' : '修改类型',
                    content: tpl,
                    area: ['500px', '190px'],
                    btn: ['确认', '取消'],
                    yes: function(index) {
                        $("#addKindForm").submit();
                    },
                    success: function(layero, layId) {
                        var row = index === undefined ? { id: '', name: '' } : kindList[index];
                        $('#name').val(row.name);
                        $("#addKindForm").validate({
                            submitHandler: function(from) {
                                common.handleAjax('kind', { code: 2, id: row.id, name: $('#name').val() }, true, true).done(function(result) {
                                    if (result.code === -4) {
                                        return;
                                    }
                                    if (result.code === -1) {
                                        toastr.error('数据异常，保存失败。');
                                        return;
                                    }
                                    if (result === 1 || result.length > 0) {
                                        toastr.success(index === undefined ? '添加成功。' : '更新成功。');
                                        layer.close(layId);
                                        initFun('wish music');
                                    } else {
                                        toastr.error(index === undefined ? '添加失败。' : '更新失败。');
                                    }
                                });
                            }
                        });
                    }
                });
            });
        },
        deleteKind: function(index) {
            layer.confirm('确定删除这个类型吗？', { btn: ['确认', '取消'] }, function(layId) {
                common.handleAjax('kind', { code: 3, id: kindList[index].id }, true, true).done(function(result) {
                    if (result.code === -1) {
                        layer.alert('该类型已被使用，无法删除。');
                        return;
                    }
                    if (result.code === -4) {
                        return;
                    }
                    if (result === 1) {
                        toastr.success('删除成功。');
                        layer.close(layId);
                        initFun('wish music');
                    } else {
                        toastr.error('删除失败。');
                    }
                });
            });
        },
        addWish: function(index) {
            require(['text!../pages/base/addWish.html'], function(tpl) {
                layer.open({
                    title: index ? '添加祝福语' : '修改祝福语',
                    content: tpl,
                    area: ['600px', '370px'],
                    btn: ['确认', '取消'],
                    yes: function(index) {
                        $("#addWishForm").submit();
                    },
                    success: function(layero, layId) {
                        var row = index === undefined ? { id: '', content: '', mini: '' } : wishList[index];
                        $('#content').val(row.content.replace(/<br>/g, '\n'));
                        $('#mini').val(row.mini.substring(0, row.mini.length - 2));
                        $('[data-toggle="tooltip"]').tooltip();
                        $("#addWishForm").validate({
                            submitHandler: function(from) {
                                var content = $('#content').val().replace(/\n/g, '<br>');
                                var mini = $('#mini').val() + '……';
                                common.handleAjax('wish', { code: 2, id: row.id, content: content, mini: mini }, true, true).done(function(result) {
                                    if (result.code === -4) {
                                        return;
                                    }
                                    if (result.code === -1) {
                                        toastr.error('数据异常，保存失败。');
                                        return;
                                    }
                                    if (result === 1 || result.length > 0) {
                                        toastr.success(index === undefined ? '添加成功。' : '更新成功。');
                                        layer.close(layId);
                                        initFun('kind music');
                                    } else {
                                        toastr.error(index === undefined ? '添加失败。' : '更新失败。');
                                    }
                                });
                            }
                        });
                    }
                });
            });
        },
        deleteWish: function(index) {
            layer.confirm('确定删除这个祝福语吗？', { btn: ['确认', '取消'] }, function(layId) {
                common.handleAjax('wish', { code: 3, id: wishList[index].id }, true, true).done(function(result) {
                    if (result.code === -1) {
                        layer.alert('该祝福语已被使用，无法删除。');
                        return;
                    }
                    if (result.code === -4) {
                        return;
                    }
                    if (result === 1) {
                        toastr.success('删除成功。');
                        layer.close(layId);
                        initFun('kind music');
                    } else {
                        toastr.error('删除失败。');
                    }
                });
            });
        },
        addMusic: function(index) {
            require(['text!../pages/base/addMusic.html'], function(tpl) {
                layer.open({
                    title: '添加音乐',
                    content: tpl,
                    area: ['500px', '300px'],
                    btn: ['确认', '取消'],
                    yes: function(index) {
                        $("#addMusicForm").submit();
                    },
                    success: function(layero, layId) {
                        var row = index === undefined ? { id: '', name: '' } : musicList[index];
                        $('#name').val(row.name);
                        $("#addMusicForm").validate({
                            submitHandler: function(from) {
                                $('#originalname').val($('#source')[0].files[0].name);
                                $('#id').val(row.id);
                                common.uploadFile('music', from, true, true).done(function(result) {
                                    if (result.code === -4) {
                                        return;
                                    }
                                    if (result.code === -1) {
                                        toastr.error('数据异常，保存失败。');
                                        return;
                                    }
                                    if (result.length > 0) {
                                        toastr.success('添加成功。');
                                        layer.close(layId);
                                        initFun('kind wish');
                                    } else {
                                        toastr.error('添加失败。');
                                    }
                                });
                            }
                        });
                    }
                });
            });
        },
        deleteMusic: function(index) {
            layer.confirm('确定删除这个音乐吗？', { btn: ['确认', '取消'] }, function(layId) {
                common.handleAjax('music', { code: 3, id: musicList[index].id, url: musicList[index].url }, true, true).done(function(result) {
                    if (result.code === -1) {
                        layer.alert('该音乐已被使用，无法删除。');
                        return;
                    }
                    if (result.code === -4) {
                        return;
                    }
                    if (result === 1) {
                        toastr.success('删除成功。');
                        layer.close(layId);
                        initFun('wish kind');
                    } else {
                        toastr.error('删除失败。');
                    }
                });
            });
        }
    }
});